package sein.android.hopenote;

import java.util.ArrayList;

import sein.android.hopenote.Const.HopeNoteDb;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class Note_Search_Tab extends ActivityGroup {
	public static Note_Search_Tab SearchTabGroup;
	private ArrayList<View> history;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		history = new ArrayList<View>();
		SearchTabGroup = this;
		
		Intent intent = new Intent(Note_Search_Tab.this,Note_Search.class);
		View view = getLocalActivityManager().startActivity("Note_Search", intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP)).getDecorView();
		
		replaceView(view);
	}
	
	// 새로운 Level의 Activity를 추가하는 경우
	 public void replaceView(View view) {
		 history.add(view);
		 setContentView(view);
	 }
		 
	 // Back Key가 눌러졌을 경우에 대한 처리
	 public void back() {
	  if(history.size() > 0) {
	   history.remove(history.size()-1);
	   if(history.size() ==  0)
	    finish();
	   else 
	    setContentView(history.get(history.size()-1));
	  } 
	  else
	  {
	   finish();
	  }
	 }
		 
		 // Back Key에 대한 Event Handler
	 @Override
	 public void onBackPressed() {
		 SearchTabGroup.back();
	  return ;
	 }

	
	/* 
	 @Override
	    public boolean onCreateOptionsMenu(Menu menu) {
	        //getMenuInflater().inflate(R.menu.activity_main, menu);
			super.onCreateOptionsMenu(menu);
			MenuItem item = null;
			menu.add(0,1,0,"사용자").setIcon(R.drawable.ic_action_search);
			menu.add(0,2,0,"검색").setIcon(R.drawable.ic_action_search);		
			return true;
	    }
		
		@Override
		public boolean onOptionsItemSelected(MenuItem item) {
			// TODO Auto-generated method stub
			//getTabHost();
			switch(item.getItemId()){
			case 1:
				Intent list = new Intent(this, User_List.class);
				startActivity(list);
				return true;
			case 2:	
				Intent item_set = new Intent(this, Item_Set.class);
				startActivityForResult(item_set, HopeNoteDb.REQ_ITEM_SET);
				return true;
				
				
				
			}
			return false;
		}
		
		
		@Override
		protected void onActivityResult(int requestCode, int resultCode, Intent data) {
			// TODO Auto-generated method stub
			super.onActivityResult(requestCode, resultCode, data);
			switch (requestCode){
			case HopeNoteDb.REQ_ITEM_SET:
				if(resultCode == RESULT_OK){
					//Intent note_search = new Intent(this, Note_Search.class);
					//note_search.putExtra(HopeNoteDb.EXTRA_SEARCH_ITEM,data.getStringExtra(HopeNoteDb.EXTRA_SEARCH_ITEM));
					//note_search.putExtra(HopeNoteDb.EXTRA_SEARCH_UNIT, data.getStringExtra(HopeNoteDb.EXTRA_SEARCH_UNIT));
					//note_search.putExtra(HopeNoteDb.EXTRA_SEARCH_CNT, data.getStringExtra(HopeNoteDb.EXTRA_SEARCH_CNT));
					//View v = Note_Search_Tab.SearchTabGroup.getLocalActivityManager().startActivity("Note_Search", note_search.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
					//Note_Search_Tab.SearchTabGroup.replaceView(v);
					
					HopeNoteDb.CURRENT_ITEM = data.getStringExtra(HopeNoteDb.EXTRA_SEARCH_ITEM);
					HopeNoteDb.CURRENT_UNIT = data.getStringExtra(HopeNoteDb.EXTRA_SEARCH_UNIT);
					HopeNoteDb.CURRENT_CNT = data.getStringExtra(HopeNoteDb.EXTRA_SEARCH_CNT);
					
					Intent intent = new Intent(Note_Search_Tab.this,Note_Search.class);
					intent.putExtra(HopeNoteDb.EXTRA_SEARCH_ITEM,data.getStringExtra(HopeNoteDb.EXTRA_SEARCH_ITEM));
					intent.putExtra(HopeNoteDb.EXTRA_SEARCH_UNIT, data.getStringExtra(HopeNoteDb.EXTRA_SEARCH_UNIT));
					intent.putExtra(HopeNoteDb.EXTRA_SEARCH_CNT, data.getStringExtra(HopeNoteDb.EXTRA_SEARCH_CNT));
					View view = getLocalActivityManager().startActivity("Note_Search", intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP)).getDecorView();
					
					history.remove(history.size()-1);
					replaceView(view);
				}
				break;
			}
		}
		*/
}
